<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="盲盒id" prop="goodsId">
        <el-input v-model="queryParams.goodsId" placeholder="请输入盲盒id" clearable />
      </el-form-item>
      <el-form-item label="产品名称" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入产品名称" clearable />
      </el-form-item>
      <el-form-item label="商品状态" prop="status">
        <el-select v-model="queryParams.status" clearable placeholder="请选择商品状态">
          <el-option v-for="item in [{
            value: '3',
            label: '售罄'
          }, {
            value: '2',
            label: '下架'
          }, {
            value: '1',
            label: '上架'
          }

          ]" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="赏类型" prop="shangId">
        <el-select v-model="queryParams.shangId" clearable placeholder="请选择赏类型" @change="handleQuery">
          <el-option v-for="item in shang_option" :key="item.label" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="时间" prop="time">
        <el-input v-model="queryParams.time" placeholder="请输入时间" clearable />
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:list:remove']">删除</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="listList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商品ID" align="center" prop="id" />
      <el-table-column label="盲盒id" align="center" prop="goodsId" />
      <el-table-column label="产品名称" align="center" prop="title" />
      <el-table-column label="产品图片" align="center" prop="imgurl">
        <template slot-scope="scope">
          <el-image :src="scope.row.imgurl" style="width: 100px; height: 120px" />
        </template>
      </el-table-column>
      <el-table-column label="背景框" align="center" prop="bgImgurl" />
      <el-table-column label="赏ID" align="center" prop="shangId">
        <template slot-scope="scope">
          {{ class_shang[scope.row.shangId] }}
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="库存" align="center" prop="stock" />
      <el-table-column label="售卖数量" align="center" prop="saleNum" />
      <el-table-column label="产品价值" align="center" prop="price" />
      <el-table-column label="回收价格" align="center" prop="money" />
      <el-table-column label="第几套" align="center" prop="num" />

      <el-table-column label="真实概率" align="center" prop="realPro" />

      <el-table-column label="保底赏概率" align="center" prop="minPro" />
      <el-table-column label="显示概率" align="center" prop="lookPro" />
      <el-table-column label="状态 1 上架 2下架 3 删除" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.status == 1">上架</el-tag>
          <el-tag type="danger" v-if="scope.row.status == 2">下架</el-tag>
          <el-tag type="warning" v-if="scope.row.status == 3">删除</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="1普通 2无限" align="center" prop="goodsType">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.goodsType == 1">普通</el-tag>
          <el-tag type="success" v-if="scope.row.goodsType == 2">无限</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="0普通 1特殊" align="center" prop="type">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.type == 0">普通</el-tag>
          <el-tag type="warning" v-if="scope.row.type == 1">特殊</el-tag>
        </template>
      </el-table-column>

      <!-- <el-table-column label="${comment}" align="center" prop="creator" /> -->
      <el-table-column label="创建时间" align="center" prop="createDate" width="180">
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.addtime, "{y}-{m}-{d} {hh}:{mm}:{ss}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:list:edit']">一键导出</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:list:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <!-- 快捷导入 -->
      <el-button type="primary" @click="importExcel">快捷导入</el-button>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="盲盒id" prop="goodsId">
          <el-input v-model="form.goodsId" placeholder="请输入盲盒id" />
        </el-form-item>
        <el-form-item label="产品名称" prop="title">
          <el-input v-model="form.title" placeholder="请输入产品名称" />
        </el-form-item>
        <el-form-item label="产品图片" prop="imgurl">
          <el-input v-model="form.imgurl" placeholder="请输入产品图片" />
        </el-form-item>
        <el-form-item label="库存" prop="stock">
          <el-input v-model="form.stock" placeholder="请输入库存" />
        </el-form-item>
        <el-form-item label="售卖数量" prop="saleNum">
          <el-input v-model="form.saleNum" placeholder="请输入售卖数量" />
        </el-form-item>
        <el-form-item label="产品价值" prop="price">
          <el-input v-model="form.price" placeholder="请输入产品价值" />
        </el-form-item>
        <el-form-item label="回收价格" prop="money">
          <el-input v-model="form.money" placeholder="请输入回收价格" />
        </el-form-item>
        <el-form-item label="第几套" prop="num">
          <el-input v-model="form.num" placeholder="请输入第几套" />
        </el-form-item>
        <el-form-item label="赏ID" prop="shangId">
          <el-input v-model="form.shangId" placeholder="请输入赏ID" />
        </el-form-item>
        <el-form-item label="${comment}" prop="addtime">
          <el-input v-model="form.addtime" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="真实概率" prop="realPro">
          <el-input v-model="form.realPro" placeholder="请输入真实概率" />
        </el-form-item>
        <el-form-item label="显示概率" prop="lookPro">
          <el-input v-model="form.lookPro" placeholder="请输入显示概率" />
        </el-form-item>
        <el-form-item label="保底赏概率" prop="minPro">
          <el-input v-model="form.minPro" placeholder="请输入保底赏概率" />
        </el-form-item>
        <el-form-item label="背景框" prop="bgImgurl">
          <el-input v-model="form.bgImgurl" placeholder="请输入背景框" />
        </el-form-item>
        <el-form-item label="${comment}" prop="creator">
          <el-input v-model="form.creator" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="createDate">
          <el-date-picker clearable v-model="form.createDate" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择${comment}">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 快捷导入 -->
    <el-dialog title="快捷导入" :visible.sync="dialogVisible" width="80%" center>
      <importForm />
    </el-dialog>
  </div>
</template>

<script>
import {
  listList,
  getList,
  delList,
  addList,
  updateList,
  getType,
} from "@/api/yfs/add_good.js";
import importForm from "@/components/importForm";
export default {
  name: "AddBoxGoods",
  props: {
    /* 编辑器的内容 */
    box_id: {
      type: String,
      default: "",
    }
  },
  data() {
    return {
      //是否打开快捷导入
      dialogVisible: false,
      //是否显示一键导入按钮
      showImport: false,
      shang_option: [],
      //赏类型对象
      class_shang: {},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      listList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        box_id: null,
        pageNum: 1,
        pageSize: 10,
        goodsId: null,
        title: null,
        imgurl: null,
        status: null,
        stock: null,
        saleNum: null,
        price: null,
        money: null,
        num: null,
        shangId: null,
        addtime: null,
        sort: null,
        realPro: null,
        lookPro: null,
        minPro: null,
        goodsType: null,
        type: null,
        bgImgurl: null,
        creator: null,
        createDate: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        goodsId: [
          { required: true, message: "盲盒id不能为空", trigger: "blur" },
        ],
        sort: [{ required: true, message: "排序不能为空", trigger: "blur" }],
        realPro: [
          { required: true, message: "真实概率不能为空", trigger: "blur" },
        ],
        lookPro: [
          { required: true, message: "显示概率不能为空", trigger: "blur" },
        ],
        minPro: [
          { required: true, message: "保底赏概率不能为空", trigger: "blur" },
        ],
        goodsType: [
          { required: true, message: "1普通 2无限不能为空", trigger: "change" },
        ],
        type: [
          { required: true, message: "0普通 1特殊不能为空", trigger: "change" },
        ],
        updateTime: [
          { required: true, message: "$comment不能为空", trigger: "blur" },
        ],
      },
    };
  },
  components: {
    importForm,
  },
  created() {
    this.queryParams.goodsId = this.$route.params.boxId || null;
    console.log(this.queryParams.box_id);
   
    
    this.getType();
    this.getList();
  },
  methods: {
    //快捷导入

    importExcel() {
      this.dialogVisible = true;
      console.log('弹窗')
    },


    //获取赏类型
    getType() {
      getType().then((res) => {
        console.log(res.rows);
        this.typeList = res.rows;
        this.typeList.forEach((item) => {
          this.class_shang[item.id] = item.title;
        });
        this.shang_option = this.typeList.map((item) => {
          return {
            value: item.id,
            label: item.title,
          };
        });
        console.log(this.class_shang);
      });
    },

    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      console.log("this.box_id", this.box_id);
      if(this.box_id){
        console.log("this.box_id", this.box_id);
        
        this.queryParams.goodsId = this.box_id;
      }

      listList(this.queryParams).then((response) => {
        this.listList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        goodsId: null,
        title: null,
        imgurl: null,
        status: 0,
        stock: null,
        saleNum: null,
        price: null,
        money: null,
        num: null,
        shangId: null,
        addtime: null,
        sort: null,
        realPro: null,
        lookPro: null,
        minPro: null,
        goodsType: null,
        type: null,
        bgImgurl: null,
        updateTime: null,
        creator: null,
        createDate: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.showImport = true;
      this.reset();
      this.open = true;
      this.title = "添加盒子商品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {

      this.reset();
      const id = row.id || this.ids;
      getList(id).then((response) => {
        this.form = response.data;

        console.log(this.form);
        //发送数据到父组件
        this.$emit("func", this.form);
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateList(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addList(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除【请填写功能名称】编号为"' + ids + '"的数据项？')
        .then(function () {
          return delList(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/list/export",
        {
          ...this.queryParams,
        },
        `list_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
